package edu.nus.cs4243.recon.ui.preview;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

import com.jreiffers.uihandlers.EventHandler;
import com.jreiffers.uihandlers.UiHandler;

import edu.nus.cs4243.recon.model.models.Model;

public class ModelPreview {
	
	private final Shell shell;	
	private final List modelList;
	private final ModelControl model;
	private final java.util.List<Model> models = new ArrayList<Model>();

	public ModelPreview(Display display) {
		shell = new Shell(display);
		modelList = new List(shell, SWT.BORDER | SWT.V_SCROLL);
		model = new ModelControl(shell, SWT.BORDER | SWT.DOUBLE_BUFFERED);
		
		shell.open();
		onResize(null);
		
		UiHandler.setup(this);
	}
	
	public void addModel(String name, Model model) {
		modelList.add(name);
		models.add(model);
	}

	public Shell getShell() {
		return shell;
	}

	@EventHandler(value = "shell", event = SWT.Close)
	public void onClose(Event event) {
		shell.dispose();
	}

	@EventHandler(value = "shell", event = SWT.Resize)
	public void onResize(Event event) {
		final Rectangle clientArea = shell.getClientArea();
		modelList.setBounds(0, 0, 150, clientArea.height);
		model.setBounds(150, 0, clientArea.width, clientArea.height);
	}
	
	@EventHandler(value = "modelList", event = SWT.Selection)
	public void onSelectModel(Event event) {
		if (modelList.getSelectionIndex() >= 0) {
			model.setCurrentModel(models.get(modelList.getSelectionIndex()));
		} else {
			model.setCurrentModel(null);
		}
	}
	
	
}
